#include <vcl.h>
#pragma hdrstop

#include "CDBEdit.h"
#pragma package(smart_init)

#define COL_DISABLE  clBtnFace
//---------------------------------------------------------------------------
// ValidCtrCheck est utilisé pour être sûr que les composants créés ne
// contiennent aucune fonction virtuelle pure.
//

static inline void ValidCtrCheck(CDBEdit *)
{
    new CDBEdit(NULL);
}
//---------------------------------------------------------------------------
__fastcall CDBEdit::CDBEdit(TComponent* Owner)
    : TDBEdit(Owner)
{
  m_asRefWebHelp                = EmptyStr;
  inherited::Visible            = true;
  ReadOnly                      = false;
  Enabled                       = true;
  m_LblFontChange               = false;
  m_Label                       = NULL;
  m_LibelleFont                 = NULL;
  m_Libelle                     = EmptyStr;
  m_SizeEspace                  = 10;
  m_pfnOnEstCorrectementRempli  = NULL;
  try {
    m_Label               = new TLabel(this);
    m_LibelleFont         = new TFont;
    m_LibelleFont->OnChange = SetLibelleFontChange;
    m_Label->Transparent  = true;
    m_Label->Visible      = false;
    m_Label->Caption      = m_Libelle;
    m_Label->Enabled      = Enabled;
    m_Label->FocusControl = this;
    //SetLibelleFont(Font);
    //SetLibellePos();
  } catch (...) {
  }
}
//---------------------------------------------------------------------------
__fastcall CDBEdit::~CDBEdit()
{
  delete m_LibelleFont;
}
//---------------------------------------------------------------------------
bool __fastcall CDBEdit::GetReadOnly(void)
{
  return TDBEdit::ReadOnly;
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetReadOnly(bool value)
{
  TDBEdit::ReadOnly = value;
  RafraichirFond(this);
}
//---------------------------------------------------------------------------
bool __fastcall CDBEdit::GetEnabled(void)
{
  return TDBEdit::Enabled;
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetEnabled(bool value)
{
  if ( m_Label) m_Label->Enabled = value;
  TDBEdit::Enabled = value;
  RafraichirFond(this);
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetLibelle(const AnsiString value)
{
  m_Libelle        = value;
  m_Label->Caption = value; SetLibellePos();
  m_Label->Visible = Visible;
  if (!m_Label->Parent) m_Label->Parent = Parent;
}



bool __fastcall CDBEdit::GetVisible() const
{
  return inherited::Visible;
}



void __fastcall CDBEdit::SetVisible( bool bVisible )
{
  inherited::Visible    = bVisible;

  if( m_Label )
      m_Label->Visible    = bVisible;
}



//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetLibellePos()
{
  m_Label->Top  = Top + ((Height - m_Label->Height - 2) >> 1);
  m_Label->Left = Left - m_Label->Width - m_SizeEspace;
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetSizeEspace(int value)
{
  m_SizeEspace = value;
  SetLibellePos();
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetLibelleFont(TFont *value)
{
  m_LibelleFont->Assign(value);
  m_LibelleFont->PixelsPerInch = value->PixelsPerInch;
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetLibelleFontMessage(TFont *value)
{
  TNotifyEvent saveptr;

  if (m_LblFontChange) return;
  saveptr = m_LibelleFont->OnChange;
  m_LibelleFont->OnChange = NULL;
  m_Label->Font->Assign(value);
  m_LibelleFont->Assign(value);
  m_Label->Font->PixelsPerInch = value->PixelsPerInch;
  m_LibelleFont->PixelsPerInch = value->PixelsPerInch;
  m_LibelleFont->OnChange = saveptr;
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetLibelleFontChange(TObject *Sender)
{
  m_Label->Font->Assign(m_LibelleFont);
  m_Label->Font->PixelsPerInch = m_LibelleFont->PixelsPerInch;
  m_LblFontChange = true;
}
//---------------------------------------------------------------------------
bool __fastcall CDBEdit::EstCorrectementRempliDefault( void)
{
  return( !Text.IsEmpty());
}
void __fastcall CDBEdit::SetObligatoire( bool bObligatoire)
{
  m_Obligatoire = bObligatoire;
  RafraichirFond(this);
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::Change( void)
{
  RafraichirFond(this);
  TDBEdit::Change();
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::WMSetFont(TWMSetFont &Msg)
{
  TDBEdit::Dispatch(&Msg);
  SetLibelleFontMessage(Font);
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::WMSetFocus( TWMSetFocus &Msg)
{
  if ( !ReadOnly) TDBEdit::Dispatch( &Msg);
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::WMWindowPosChanged(TWMWindowPosChanged &Msg)
{
  TDBEdit::Dispatch(&Msg);
  SetLibellePos();
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::WMChar( TWMChar &Msg)
{
  if(( Msg.CharCode=='.') &&
     ( dynamic_cast<TFloatField*>( Field) || dynamic_cast<TCurrencyField*>( Field))) Msg.CharCode = DecimalSeparator;
  inherited::Dispatch( &Msg);
}
//---------------------------------------------------------------------------
namespace Cdbedit
{
    void __fastcall PACKAGE Register()
    {
        TComponentClass classes[1] = {__classid(CDBEdit)};
        RegisterComponents("EIC CtrlDB", classes, 0);
    }
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::SetBleuSiHint( bool bBleuSiHint)
{
  m_BleuSiHint = bBleuSiHint;
  RafraichirFond(this);
}
//---------------------------------------------------------------------------
void __fastcall CDBEdit::KeyDown( Word &Key, TShiftState Shift)
{
    if( Key == VK_F3 )
        VerifierRefWebHelp( m_asRefWebHelp );
}
//---------------------------------------------------------------------------
